package com.zxkj.commonpatrol.viewmodel;

import android.app.Application;

import androidx.annotation.NonNull;
import androidx.databinding.ObservableField;

import com.czy.basecommon.utils.base.BaseRepository;
import com.czy.basecommon.utils.base.BaseViewModel;
import com.czy.basecommon.utils.base.StateLiveData;
import com.czy.basecommon.utils.binding.command.BindingAction;
import com.czy.basecommon.utils.binding.command.BindingCommand;
import com.zxkj.commonpatrol.model.response.PointCategoryResponse;
import com.zxkj.commonpatrol.model.response.PointInfoResponse;
import com.zxkj.commonpatrol.repoisity.AddCheckPointRepoisity;
import com.zxkj.commonpatrol.repoisity.NewExceptionRepoisity;

import java.util.ArrayList;
import java.util.List;

public class AddCheckPointViewModel extends BaseViewModel<AddCheckPointRepoisity> {
    public StateLiveData<List<PointCategoryResponse>>TypeList =new StateLiveData<>();
    public StateLiveData<List<PointCategoryResponse>>TypeDetail =new StateLiveData<>();
    public StateLiveData<Object>TypeSave =new StateLiveData<>();
    public StateLiveData<String>CategoryCode =new StateLiveData<>();
    public ObservableField<String> PointNo = new ObservableField<>("");

    public ObservableField<String> PointType = new ObservableField<>("");

    public ObservableField<String> EName = new ObservableField<>("");

    public ObservableField<String> CName = new ObservableField<>("");

    public ObservableField<String> Loc = new ObservableField<>("");

    public ObservableField<String> Time = new ObservableField<>("");

    public ObservableField<String> RL = new ObservableField<>("");
    public ObservableField<String> ZB = new ObservableField<>("");

    public AddCheckPointViewModel(@NonNull Application application) {
        super(application);
    }


    public  void  getTypeList(String Code){
        mRepository.getTypeList(Code,new BaseRepository.MyBaseDataCallBack() {
            @Override
            public void onDataSuccessCallBack(Object o) {
                List<PointCategoryResponse> datas=(List<PointCategoryResponse> )o;
                if (datas==null){
                    datas=new ArrayList<>();
                }
                TypeList.postSuccess(datas);
            }

            @Override
            public void onDataFailCallBack(Object o, Object t1) {
                TypeList.postFailure(404,(String)t1);
            }
        });
    }


    public void SeeDetail(String  Code){
        mRepository.getTypeDetail(Code, new BaseRepository.MyBaseDataCallBack() {
            @Override
            public void onDataSuccessCallBack(Object o) {
                PointInfoResponse bean=(PointInfoResponse)o;
                if (bean!=null){
                    PointNo.set(bean.Code);
                    PointType.set(bean.CategoryName);
                    EName.set(bean.EName);
                    CName.set(bean.CName);
                    Loc.set(bean.Location);
                    Time.set(bean.ExpiryDate);
                    ZB.set(bean.Group);
                    RL.set(bean.Capacity);
                    CategoryCode.postSuccess(bean.CategoryNo);

                }

                List<PointCategoryResponse> da=new ArrayList<>();
                TypeDetail.postSuccess(da);

            }

            @Override
            public void onDataFailCallBack(Object o, Object t1) {
                TypeDetail.postFailure(404,(String)t1);
            }
        });

    }

    public  void Save(String CategoryCode, String RouteCode, int isAdd, String routeCode){
        PointInfoResponse response =new PointInfoResponse();
        response.setCategoryName(PointType.get());
        response.setCategoryNo(CategoryCode);
        response.setCName(CName.get());
        response.setExpiryDate(Time.get());
        response.setCode(PointNo.get());
        response.setLocation(Loc.get());
        if (isAdd==0){
            response.setRouteCode(routeCode);
        }else{
            response.setRouteCode(RouteCode);
        }


        response.setEName(EName.get());
        response.setCapacity(RL.get());
        response.setGroup(ZB.get());
        response.setIsAdd(isAdd);
        mRepository.Save(response, new BaseRepository.MyBaseDataCallBack() {
            @Override
            public void onDataSuccessCallBack(Object o) {
                TypeSave.postSuccess(o);
            }

            @Override
            public void onDataFailCallBack(Object o, Object t1) {
                TypeSave.postFailure(404,(String)t1);
            }
        });

    }


}
